﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace ModbusReadAndWrite.Protocol
{
    public enum ProtocolType
    {
        [Description("RTU")]
        ModbusRTUProtocol,
        [Description("ASCII")]
        ModbusASCProtocol,
        [Description("TCP")]
        ModbusTCPProtocol
    }

    #region UserProtocol
    /// <summary>
    /// 自定义协议封装
    /// </summary>
    class UserProtocol
    {
        [Description("是否Master连接，默认true")]
        public bool IsMasterConn { get; set; } = true;

        //master接收到slave响应报文时间 ms，当IsMasterConn为true时生效
        public int ResponseTime { get; set; } = 1000;
        //master向slave发送多个poll的时间间隔 ms，当IsMasterConn为true时生效
        public int DelayBetweenPollsTime { get; set; } = 20;

        //数据字节长度
        public int DataLength { get; set; }

        public UserProtocol(int dataLength)
        {
            this.DataLength = dataLength;
        }

        public UserProtocol(int responseTime, int delayBetweenPollsTime, int dataLength)
        {
            this.ResponseTime = responseTime;
            this.DelayBetweenPollsTime = delayBetweenPollsTime;
            this.DataLength = dataLength;
        }

        public byte[] ReadCoils()
        {
            byte[] readData;
            return new byte[] { };
        }

        public byte[] ReadDiscreteInputs()
        {
            byte[] readData;
            return new byte[] { };
        }

        public byte[] ReadHoldingRegisters()
        {
            return new byte[] { };
        }

        public byte[] ReadInputRegisters()
        {
            return new byte[] { };
        }

        public void WriteSingleCoils()
        {

        }

        public void WriteSingleRegister()
        {

        }

        public void WriteMultipleCoils()
        {

        }

        public void WriteMultipleRegisters()
        {

        }
    }
    #endregion

    #region ICommonProtocol
    /// <summary>
    /// 协议接口
    /// </summary>
    interface ICommonProtocol
    {/*
        void Open();
        void Connect();
        void Send();
        void Disconnect();
        void Close();*/
    }
    #endregion

    #region TCP
    /// <summary>
    /// TCP协议
    /// </summary>
    class TCPProtocol : ICommonProtocol
    {
        //建立socket链接时需要的字段
        public string IpAddress { get; set; } = "127.0.0.1";
        public int Port { get; set; } = 502;
        public int Timeout { get; set; } = 3000;
        public string IpType { get; set; } = "IPv4";
        public bool IsLongTimeCon { get; set; } = false;
        public int KeepTimeout { get; set; } = 30000;//心跳包监听连接状态，当断开连接时需在状态栏显示断开连接

        //写单个寄存器数据时需要添加的字段
        public int SlaveID { get; set; } = 1;
        public int StartAddress { get; set; } = 0;
        public int Value { get; set; } = 0;

        //建立TCP连接客户端
        public TcpClient tcpClient = null;

        public TCPProtocol() {}

        public TCPProtocol(string ipAddress, int port, int timeout, string ipType, bool isLongTimeCon, int keepTimeout)
        {
            this.IpAddress = ipAddress;
            this.Port = port;
            this.Timeout = timeout;
            this.IpType = ipType;
            this.IsLongTimeCon = isLongTimeCon;
            this.KeepTimeout = keepTimeout;
        }

        

        public void Disconnect()
        {
            tcpClient.Dispose();
        }
    }
    #endregion

    #region Modbus RTU ASCII
    /// <summary>
    /// Modbus协议
    /// </summary>
    class ModbusProtocol : ICommonProtocol
    {
        private int _protocolType;

        public ModbusProtocol(int protocolType)
        {
            this._protocolType = protocolType;
        }

        public int ProtocolType { get; }

        
    }
    #endregion
}
